<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of AjaxAction
 *
 * @author 敏学
 */
class AjaxAction extends AjaxPublicAction{
    //put your code here
    /// 获取产品信息(流程使用)
	public function getProductInfo() {
		$id 			= intval($_GET['id']);
		$color_id		= intval($_GET['color_id']); /// 颜色
		$size_id		= intval($_GET['size_id']); /// 尺码
		$currency_id	= intval($_GET['currency_id']); /// 根据币别取单价
		$flow			= trim($_GET['flow']);
		$type			= trim($_GET['type']);
		$mantissa		= trim($_GET['mantissa']);
		$adjust_date	= $_GET['adjust_date'];
		if ($id > 0 ) {
			$product = M("Product")->field('product_name,instock_price,sale_price,wholesale_price,retail_price,capability,dozen,weight,(cube_high*cube_wide*cube_long) as per_size')->select($id);
			$info 	 = $product[0];
			$info['autocomplete'] = false;
			if ($flow=='sale' || $type == 'sale_return') {
				$info['storage_color']								= 0;
				$info['storage_size']								= 0;
				$where												= ' and 1';
				if(C('storage_color')==1) $info['storage_color']	= 1;
				if(C('storage_size')==1)  $info['storage_size']		= 1;
				if($color_id>0) $where	.= ' and color_id='.$color_id;
				if($size_id>0) $where	.= ' and size_id='.$size_id;
				$temp = M('SaleStorage')->where('product_id='.$id.$where.' and quantity>0 and mantissa='.$mantissa)->select();
				if (count($temp)==1) {
					$info['capability'] 	= $temp[0]['capability'];
					$info['dozen'] 			= $temp[0]['dozen'];
				}else if(empty($temp)){
					$info['capability']		= '';
					$info['dozen']			= '';
					$info['autocomplete'] 	= false;
				}else {
					$info['capability'] 	= 0;
					$info['dozen'] 			= 0;
					$info['autocomplete'] 	= true;
				}
			}else {
				if (C('storage_format')==3) {
					$info['capability'] = $info['capability']/$info['dozen'];
				}
			}
			if($flow=='adjust'){
				if(C('currency')<>1){
					$date	= date('Y-m-d',strtotime(formatDate($adjust_date)));
					$rate 	= M('rate_info')
								->where("rate_date='".$date."' and from_currency_id=1 and to_currency_id=".C('currency'))
								->find();	
					if(!$rate['opened_y']){
						$fixed_rate	= M('fixed_rate')->where('from_currency_id=1 and to_currency_id='.C('currency'))->find();
						$info['instock_price']	= $info['instock_price']*$fixed_rate['rate'];
					}else{
						$info['instock_price']	= $info['instock_price']*$rate['opened_y'];
					}
				}
			}else{
				if ($currency_id <> 1) { /// 只有币种是人民币时才取预计进货单价
					$info['instock_price'] 	= 0;
				} 
			}
			/// 多客户类型
			if (C('MULTI_CLIENT') && $_GET['client_id'] > 0) {
				$client_type = M("Company")->where('comp_type=1 and id='.intval($_GET['client_id']))->find();
				$client_type = $client_type['detail_type'];
				$info['sale_price'] = $client_type == 1 ? $info['wholesale_price'] :
				( $client_type == 2 ? $info['retail_price'] : $info['sale_price']);
			}
			$field 		= 'last_price_'.$currency_id;
			$field_avg 	= 'avg_price_'.$currency_id;
			if (C('price_default') == 2) { /// 最近一次入库/销售价
				$last_price 			= M("ProductPrice")->where('product_id='.$id)->select();
				$info['instock_price'] 	= $last_price[$field] == null ? 0 : $last_price[$field];
				$info['sale_price'] 	= $last_price['sale_'.$field] == null ? 0 : $last_price['sale_'.$field];
			} else if (C('price_default') == 3) { /// 平均入库/销售单价
				$avg_price 				= M("ProductPrice")->where('product_id='.$id)->select();
				$info['instock_price'] 	= $avg_price[$field_avg] == null ? 0 : $avg_price[$field_avg];
				$info['sale_price'] 	= $avg_price['sale_'.$field_avg] == null ? 0 : $avg_price['sale_'.$field_avg];
			}
			/// 销售时币种与本位币不一致时取
			if ($currency_id <> C('CURRENCY')) {
				$info['sale_price']		= 0;
			}
			$info['show_delivery']		= 0;
			if(C('instock.delivery')==2){
				$info['show_delivery']		= 1;
			}
			$info['capability']==0 && $info['capability'] 		= '';
			$info['dozen']==0 && $info['dozen'] = '';
			$info = _formatArray($info);
                        $info['sale_price']==0 && $info['sale_price']   = $info['edml_sale_price'] = '';
			echo json_encode($info);
			exit();
		}
		exit();
	}
}

?>
